﻿using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;

namespace ERP.Web.Controllers
{
    public class InventoryDataController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取仓库列表
        /// </summary>
        /// <returns>仓库列表</returns>
        [HttpGet]
        public JsonResult GetWarehouses()
        {
            // 模拟数据，实际项目中应该从数据库获取
            var warehouses = new List<object>
            {
                new { Id = "", Name = "全部" },
                new { Id = "1", Name = "原材料仓库" },
                new { Id = "2", Name = "半成品仓库" },
                new { Id = "3", Name = "成品仓库" },
                new { Id = "4", Name = "天中港项目" },
                new { Id = "5", Name = "虎地功库" }
            };

            return Json(new { code = 0, msg = "", data = warehouses });
        }

        /// <summary>
        /// 获取库存统计数据
        /// </summary>
        /// <param name="warehouseId">仓库ID</param>
        /// <returns>库存统计数据</returns>
        [HttpGet]
        public JsonResult GetInventoryStats(string warehouseId = "")
        {
            // 模拟数据，实际项目中应该从数据库获取
            var stats = new
            {
                total = 2190636,
                types = 25,
                count = 6,
                expiring = 1
            };

            return Json(new { code = 0, msg = "", data = stats });
        }

        /// <summary>
        /// 获取库存数量数据（用于柱状图）
        /// </summary>
        /// <param name="warehouseId">仓库ID</param>
        /// <returns>库存数量数据</returns>
        [HttpGet]
        public JsonResult GetInventoryBarData(string warehouseId = "")
        {
            // 模拟数据，实际项目中应该从数据库获取
            object barData;

            switch (warehouseId)
            {
                case "1": // 原材料仓库
                    barData = new
                    {
                        labels = new[] { "飞雁 - R10,PG-01", "PE - 20*30*40", "ET - 20*20*20", "PP - 50*50*50", "TBC - 20*20" },
                        values = new[] { 999990, 10036, 10022, 1000, 950 }
                    };
                    break;
                case "2": // 半成品仓库
                    barData = new
                    {
                        labels = new[] { "测试-2.4G蓝牙/接收信号灯", "PE - 20*30*40", "ET - 20*20*20", "PP - 50*50*50", "9999" },
                        values = new[] { 1000000, 10036, 10022, 50, 20 }
                    };
                    break;
                case "3": // 成品仓库
                    barData = new
                    {
                        labels = new[] { "PE - 20*30*40", "鞍钢 - R5,KL-01" },
                        values = new[] { 60, 10 }
                    };
                    break;
                case "4": // 天中港项目
                    barData = new
                    {
                        labels = new[] { "测试 - 测" },
                        values = new[] { 3 }
                    };
                    break;
                case "5": // 虎地功库
                    barData = new
                    {
                        labels = new[] { "颗粒配成品盒" },
                        values = new[] { 2 }
                    };
                    break;
                default: // 全部
                    barData = new
                    {
                        labels = new[] { "PE - 20*30*40", "ET - 20*20*20", "PP - 50*50*50", "TBC - 20*20", "霞珠灯 - 4CM" },
                        values = new[] { 59894, 38578, 20900, 10000, 8000 }
                    };
                    break;
            }

            return Json(new { code = 0, msg = "", data = barData });
        }

        /// <summary>
        /// 获取库存物料分布数据（用于饼图）
        /// </summary>
        /// <param name="warehouseId">仓库ID</param>
        /// <returns>库存物料分布数据</returns>
        [HttpGet]
        public JsonResult GetInventoryPieData(string warehouseId = "")
        {
            // 模拟数据，实际项目中应该从数据库获取
            List<object> pieData;

            switch (warehouseId)
            {
                case "1": // 原材料仓库
                    pieData = new List<object>
                    {
                        new { value = 999990, name = "飞雁 - R10,PG-01 (85.83%)" },
                        new { value = 10036, name = "PE - 20*30*40 (0.98%)" },
                        new { value = 10022, name = "ET - 20*20*20 (0.98%)" },
                        new { value = 1000, name = "PP - 50*50*50 (0.1%)" },
                        new { value = 950, name = "TBC - 20*20 (0.09%)" },
                        new { value = 123638, name = "其他 (12.02%)" }
                    };
                    break;
                case "2": // 半成品仓库
                    pieData = new List<object>
                    {
                        new { value = 1000000, name = "测试-2.4G蓝牙/接收信号灯 (97.51%)" },
                        new { value = 10036, name = "PE - 20*30*40 (0.98%)" },
                        new { value = 10022, name = "ET - 20*20*20 (0.98%)" },
                        new { value = 50, name = "PP - 50*50*50 (0.01%)" },
                        new { value = 20, name = "9999 (0.01%)" },
                        new { value = 5200, name = "其他 (0.51%)" }
                    };
                    break;
                case "3": // 成品仓库
                    pieData = new List<object>
                    {
                        new { value = 60, name = "PE - 20*30*40 (85.71%)" },
                        new { value = 10, name = "鞍钢 - R5,KL-01 (14.29%)" }
                    };
                    break;
                case "4": // 天中港项目
                    pieData = new List<object>
                    {
                        new { value = 3, name = "测试 - 测 (100%)" }
                    };
                    break;
                case "5": // 虎地功库
                    pieData = new List<object>
                    {
                        new { value = 2, name = "颗粒配成品盒 (100%)" }
                    };
                    break;
                default: // 全部
                    pieData = new List<object>
                    {
                        new { value = 59894, name = "PE - 20*30*40 (5.14%)" },
                        new { value = 38578, name = "ET - 20*20*20 (3.4%)" },
                        new { value = 20900, name = "PP - 50*50*50 (1.8%)" },
                        new { value = 10000, name = "TBC - 20*20 (0.86%)" },
                        new { value = 2061264, name = "其他 (88.8%)" }
                    };
                    break;
            }

            return Json(new { code = 0, msg = "", data = pieData });
        }
    }
}
